package com.facebook.games.fa.controller
{
	import com.facebook.games.fa.business.ChooseTeamBO;
	import com.facebook.games.fa.events.ChooseTeamEvent;
	import com.facebook.games.fa.events.GameEvent;
	import com.facebook.games.fa.view.ChooseTeam;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * Controlador da ChoseTeam view
	 * @author Diego Maehler <dmaehler@gmail.com>
	 */
	public class ChooseTeamController
	{
		
		/**
		 * ChooseTeam view
		 */
		public var view : ChooseTeam;
		
		private var teamBO : ChooseTeamBO = new ChooseTeamBO();
		
		[Bindable]
		public var teams : ArrayCollection;
		
		/**
		 * Inicia o jogo
		 */
		public function begin () : void
		{
			this.view.dispatchEvent(new GameEvent(GameEvent.BEGIN_GAME));
		}
		
		/**
		 * Carrega as equipes para escolher os baralhos
		 */
		public function getTeams () : void
		{
			this.view.states
			this.teamBO.addEventListener(ChooseTeamEvent.TEAMS_LOADED, this.onTeamsLoaded);
			this.teamBO.addEventListener(ChooseTeamEvent.LOAD_ERROR, this.onLoadError);
			this.teamBO.loadTeams();
		}
		
		/**
		 * Handler para quando carregar as equipes
		 */
		protected function onTeamsLoaded(event:ChooseTeamEvent):void
		{
			this.teams = this.teamBO.teams;
		}
		
		/**
		 * Handler caso ocorra um erro ao carregar as equipes
		 */
		protected function onLoadError(event:ChooseTeamEvent):void
		{
			this.teams = null;
		}
				
	}
}